package com.demandbreakdown.xiyun.model.vo.space;

import com.demandbreakdown.xiyun.model.entity.Space;
import com.demandbreakdown.xiyun.model.vo.user.UserVO;
import io.swagger.v3.oas.annotations.media.Schema;
import lombok.AllArgsConstructor;
import lombok.Builder;
import lombok.Data;
import lombok.NoArgsConstructor;

import java.time.LocalDateTime;

/**
 * 空间信息VO
 *
 * @author poxz
 */
@Data
@Builder
@NoArgsConstructor
@AllArgsConstructor
@Schema(description = "空间信息VO")
public class SpaceVO {
    @Schema(description = "空间ID", example = "123")
    private Long id;

    @Schema(description = "空间名称", example = "我的工作空间")
    private String spaceName;

    @Schema(description = "空间描述", example = "这是一个用于存储工作相关图片的空间")
    private String spaceDesc;

    @Schema(description = "空间类型：0-私有空间，1-团队空间", example = "0")
    private Integer spaceType;

    @Schema(description = "空间级别：0-普通版，1-高级版，2-至尊版", example = "0")
    private Integer spaceLevel;

    @Schema(description = "空间封面图URL", example = "https://oss/cover.jpg")
    private String coverUrl;

    @Schema(description = "最大图片数量", example = "1000")
    private Integer maxPictureCount;

    @Schema(description = "最大存储空间（字节）", example = "1073741824")
    private Long maxStorageSize;

    @Schema(description = "邀请码（团队空间专用）", example = "A1B2C3D4")
    private String inviteCode;

    @Schema(description = "成员上限", example = "10")
    private Integer memberLimit;

    @Schema(description = "创建用户ID", example = "456")
    private Long userId;

    @Schema(description = "当前用户在空间中的角色", example = "owner")
    private String currentUserRole;

    @Schema(description = "空间使用情况统计")
    private SpaceUsageVO usage;

    @Schema(description = "创建用户信息")
    private UserVO user;

    @Schema(description = "创建时间", example = "2025-09-20T12:00:00Z")
    private LocalDateTime createTime;

    public static SpaceVO from(Space space) {
        if (space == null) {
            return null;
        }
        return SpaceVO.builder()
                .id(space.getId())
                .spaceName(space.getSpaceName())
                .spaceDesc(space.getSpaceDesc())
                .spaceType(space.getSpaceType())
                .spaceLevel(space.getSpaceLevel())
                .coverUrl(space.getCoverUrl())
                .maxPictureCount(space.getMaxPictureCount())
                .maxStorageSize(space.getMaxStorageSize())
                .inviteCode(space.getInviteCode())
                .memberLimit(space.getMemberLimit())
                .userId(space.getUserId())
                .createTime(space.getCreateTime())
                .build();
    }

    public static SpaceVO from(Space space, String currentUserRole, SpaceUsageVO usage) {
        SpaceVO spaceVO = from(space);
        if (spaceVO != null) {
            spaceVO.setCurrentUserRole(currentUserRole);
            spaceVO.setUsage(usage);
        }
        return spaceVO;
    }

    public static SpaceVO from(Space space, UserVO user, String currentUserRole, SpaceUsageVO usage) {
        SpaceVO spaceVO = from(space, currentUserRole, usage);
        if (spaceVO != null) {
            spaceVO.setUser(user);
        }
        return spaceVO;
    }
}
